﻿// <copyright file="InformModemStatus.cs" company="Hallvard Korsgaard">
//  Microsoft Public License (Ms-PL). See license details here: http://enoceannet.codeplex.com/license
// </copyright>
// <author>Hallvard Korsgaard</author> 

namespace EnOcean.Message
{
  using System;

  /// <summary>
  /// Informs the user about the TCM 120 current modem status. The information provided is the following: Modem status (ON or OFF) and modem ID stored.
  /// </summary>
  [Serializable]
  public class InformModemStatus : MessageTelegram
  {
    /// <summary>
    /// Variable for the ModemId property.
    /// </summary>
    private int modemId;

    /// <summary>
    /// Gets a value indicating whether the modem is on or off. True = On, false = Off
    /// </summary>
    public bool IsModemOn
    {
      get;
      internal set;
    }

    /// <summary>
    /// Gets or sets the 16 bit modem ID. The ID must be in the range 0 to 0xffff inclusive.
    /// </summary>
    public int ModemId
    {
      get
      {
        return this.modemId;
      }

      set
      {
        if (value >= 0x0 && value <= 0xffff)
        {
          this.modemId = value;
        }
        else
        {
          throw new ArgumentOutOfRangeException("ModemId", "must be a 16 bit value in the range 0x0 to 0xffff inclusive");
        }
      }
    }

    /// <summary>
    /// Creates the byte format of the message.
    /// </summary>
    /// <param name="message">A byte array of length 14 to hold the message contents.</param>
    protected internal override void CreateMessage(byte[] message)
    {
      byte[] i;

      base.CreateMessage(message);

      i = BitConverter.GetBytes(this.ModemId);
      message[3] = (byte)MessageType.InformModemStatus;
      message[4] = (byte)(this.IsModemOn ? 1 : 0); // Modem state 1 = On , 0 = off 
      message[5] = i[1]; // Id MSB
      message[6] = i[0]; // Id LSB
    }
  }
}
